<?php

namespace App\HttpController\XApp\Reply\Admin;


use App\Common\Log;
use App\Common\OA\OA;
use App\Common\SysEnum;
use App\HttpController\ControllerAdmin;
use App\Model\t_wx_reply;
use XWX\Common\H;
use XWX\Common\Validate\Validate;

class Reply extends ControllerAdmin
{
    public function index()
    {
        $compKw = $this->reqParam('compKw');


        $query = $this->db()
            ->where('reply_x_id', $this->funcGetX())
            ->where('reply_app_id', 'reply')
            ->orderBy('reply_create_date');

        if (H::funcStrHasAnyText($compKw))
        {
            $query->where('reply_name', "%{$compKw}%", 'LIKE');
        }

        $query_count = clone $query;

        $list = $query->get(t_wx_reply::table(), [$this->funcGetPageIndexXPage(), $this->funcGetPageSize()]);
        $count = $query_count->count(t_wx_reply::table());


        //二次加工
        $list_ = [];
        foreach ($list as $v)
        {
            $data = $v;

            $data['id'] = $v['reply_id'];
            $data['type'] = SysEnum::funcGet("reply_type.{$v['reply_type']}");
            $data['type'] = $data['type'] . '回复';

            $list_[] = $data;
        }


        $this->commonR()->setData("list", $list_);
        $this->commonR()->setData("count", $count);


        $this->funcWriteCommonR();
    }


    public function edit()
    {
        $this->funcSetTitle("回复编辑");

        $data = [
            'reply_key' => time(),
            'reply_type' => 'msg',
            'reply_redirect_type' => '',
            'reply_pic_imgs' => [],
            'is_app' => false,
        ];
        if ($this->funcIsAdminEdit())
        {
            /** @var t_wx_reply $entity */
            $entity = t_wx_reply::funcXLoad($this->db(), $this->funcGetX(), $this->reqParam('id'));


            $data_append = [
                'id' => $entity->funcGetID(),
                'reply_pic_imgs' => OA::funcGetUploadFileList($entity->reply_pic),
                'is_app' => $entity->reply_app_id != "reply",
            ];
            $data = array_merge($entity->toArray(), $data_append);
        }

        $this->commonR()->setData('data', $data);


        $this->funcWriteCommonR();
    }

    public function save()
    {
        $v = new Validate();
        $v->addColumn('reply_key')
            ->required()
            ->func(function ($val)//验证key是否存在
            {
                Log::funcLog(__METHOD__, '验证key是否存在');
                $query = $this->db()
                    ->where('reply_x_id', $this->funcGetX())
                    ->where('reply_key', $val);
                if ($this->funcIsAdminEdit())
                {
                    $query->where('reply_id', $this->reqParam('reply_id'), '<>');
                }

                $count = $query->count(t_wx_reply::table());
                Log::funcLog(__METHOD__, $count);
                if ($count > 0)
                {
                    return $this->funcGetR(-1071, '键已存在');
                }

                return $this->funcGetR(0);
            });
        $v->addColumn('reply_name')->required();
        $v->addColumn('reply_type')->required();
        $v->addColumn('reply_desc')
            ->required()
            ->lenMax(500);

        if ($this->reqParam('reply_type') === 'msg')
        {
            $v->addColumn('reply_title')
                ->required()
                ->lenMax(50);
            $v->addColumn('reply_redirect')
                ->required()
                ->lenMax(200);
        }

        if (!$v->validate($this->reqParam(), true))
        {
            return $this->funcWriteValidate($v->getErrors());
        }


        $ignore_field = [];
        if ($this->funcIsAdminEdit())
        {
            $ignore_field = ['reply_app_id'];
        }


        $this->funcSave(t_wx_reply::class, $ignore_field, false);


        $this->funcWriteCommonR();
    }

    public function funcSaveBefore($entity)
    {
        if ($this->funcIsAdminAdd())
        {
            $entity->reply_app_id = 'reply';
        }
    }


    //删除
    public function del()
    {
        $ids = $this->reqParam('ids');

        $r = $this->funcDel(t_wx_reply::class, $ids);


        $this->funcWriteErrR($r);
    }
}